Option Strict On
Option Explicit On 

Imports System.Windows.Forms
Imports System.Diagnostics

Imports IP.Core.IPdATA
Imports IP.Core.IPUserService
Imports IP.Core.IPCommon
Imports IP.Core.IPException
Imports IP.Core.IPDataControl
Imports C1.Win.C1FlexGrid

Class f108_phan_quyen_he_thong
    Inherits System.Windows.Forms.Form
    Implements I_InplaceEditGridContainer, I_DisplayableInPanel



#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        formatControls()
    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents imageList As System.Windows.Forms.ImageList
    Friend WithEvents m_fg As C1.Win.C1FlexGrid.C1FlexGrid
    Friend WithEvents m_cmd_xoa As SIS.Controls.Button.SiSButton
    Friend WithEvents m_cmd_thoat As SIS.Controls.Button.SiSButton
    Friend WithEvents m_cmd_cap_nhat As SIS.Controls.Button.SiSButton
    Friend WithEvents m_panel As System.Windows.Forms.Panel
    Friend WithEvents m_stb_fms_style As System.Windows.Forms.StatusBar
    Friend WithEvents m_stMess As System.Windows.Forms.StatusBarPanel
    Friend WithEvents m_stUser As System.Windows.Forms.StatusBarPanel
    Friend WithEvents m_cmd_undo As SIS.Controls.Button.SiSButton
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(f108_phan_quyen_he_thong))
        Me.m_fg = New C1.Win.C1FlexGrid.C1FlexGrid
        Me.m_panel = New System.Windows.Forms.Panel
        Me.m_cmd_xoa = New SIS.Controls.Button.SiSButton
        Me.imageList = New System.Windows.Forms.ImageList(Me.components)
        Me.m_cmd_cap_nhat = New SIS.Controls.Button.SiSButton
        Me.m_cmd_undo = New SIS.Controls.Button.SiSButton
        Me.m_cmd_thoat = New SIS.Controls.Button.SiSButton
        Me.m_stb_fms_style = New System.Windows.Forms.StatusBar
        Me.m_stMess = New System.Windows.Forms.StatusBarPanel
        Me.m_stUser = New System.Windows.Forms.StatusBarPanel
        CType(Me.m_fg, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.m_panel.SuspendLayout()
        CType(Me.m_stMess, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.m_stUser, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'm_fg
        '
        Me.m_fg.AllowAddNew = True
        Me.m_fg.AllowDragging = C1.Win.C1FlexGrid.AllowDraggingEnum.Rows
        Me.m_fg.ColumnInfo = resources.GetString("m_fg.ColumnInfo")
        Me.m_fg.Dock = System.Windows.Forms.DockStyle.Fill
        Me.m_fg.KeyActionEnter = C1.Win.C1FlexGrid.KeyActionEnum.MoveAcross
        Me.m_fg.Location = New System.Drawing.Point(0, 0)
        Me.m_fg.Name = "m_fg"
        Me.m_fg.Rows.Count = 32
        Me.m_fg.Size = New System.Drawing.Size(551, 355)
        Me.m_fg.Styles = New C1.Win.C1FlexGrid.CellStyleCollection(resources.GetString("m_fg.Styles"))
        Me.m_fg.TabIndex = 0
        '
        'm_panel
        '
        Me.m_panel.Controls.Add(Me.m_cmd_xoa)
        Me.m_panel.Controls.Add(Me.m_cmd_cap_nhat)
        Me.m_panel.Controls.Add(Me.m_cmd_undo)
        Me.m_panel.Controls.Add(Me.m_cmd_thoat)
        Me.m_panel.Dock = System.Windows.Forms.DockStyle.Bottom
        Me.m_panel.Location = New System.Drawing.Point(0, 355)
        Me.m_panel.Name = "m_panel"
        Me.m_panel.Size = New System.Drawing.Size(551, 34)
        Me.m_panel.TabIndex = 1
        '
        'm_cmd_xoa
        '
        Me.m_cmd_xoa.AdjustImageLocation = New System.Drawing.Point(0, 0)
        Me.m_cmd_xoa.BtnShape = SIS.Controls.Button.emunType.BtnShape.Rectangle
        Me.m_cmd_xoa.BtnStyle = SIS.Controls.Button.emunType.XPStyle.[Default]
        Me.m_cmd_xoa.Dock = System.Windows.Forms.DockStyle.Right
        Me.m_cmd_xoa.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.m_cmd_xoa.ImageIndex = 4
        Me.m_cmd_xoa.ImageList = Me.imageList
        Me.m_cmd_xoa.Location = New System.Drawing.Point(231, 0)
        Me.m_cmd_xoa.Name = "m_cmd_xoa"
        Me.m_cmd_xoa.Size = New System.Drawing.Size(72, 34)
        Me.m_cmd_xoa.TabIndex = 18
        Me.m_cmd_xoa.Text = "&Xoá "
        '
        'imageList
        '
        Me.imageList.ImageStream = CType(resources.GetObject("imageList.ImageStream"), System.Windows.Forms.ImageListStreamer)
        Me.imageList.TransparentColor = System.Drawing.Color.Transparent
        Me.imageList.Images.SetKeyName(0, "")
        Me.imageList.Images.SetKeyName(1, "")
        Me.imageList.Images.SetKeyName(2, "")
        Me.imageList.Images.SetKeyName(3, "")
        Me.imageList.Images.SetKeyName(4, "")
        Me.imageList.Images.SetKeyName(5, "")
        Me.imageList.Images.SetKeyName(6, "")
        Me.imageList.Images.SetKeyName(7, "")
        Me.imageList.Images.SetKeyName(8, "")
        '
        'm_cmd_cap_nhat
        '
        Me.m_cmd_cap_nhat.AdjustImageLocation = New System.Drawing.Point(0, 0)
        Me.m_cmd_cap_nhat.BtnShape = SIS.Controls.Button.emunType.BtnShape.Rectangle
        Me.m_cmd_cap_nhat.BtnStyle = SIS.Controls.Button.emunType.XPStyle.[Default]
        Me.m_cmd_cap_nhat.Dock = System.Windows.Forms.DockStyle.Right
        Me.m_cmd_cap_nhat.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.m_cmd_cap_nhat.ImageIndex = 0
        Me.m_cmd_cap_nhat.ImageList = Me.imageList
        Me.m_cmd_cap_nhat.Location = New System.Drawing.Point(303, 0)
        Me.m_cmd_cap_nhat.Name = "m_cmd_cap_nhat"
        Me.m_cmd_cap_nhat.Size = New System.Drawing.Size(80, 34)
        Me.m_cmd_cap_nhat.TabIndex = 17
        Me.m_cmd_cap_nhat.Text = "&Lưu"
        '
        'm_cmd_undo
        '
        Me.m_cmd_undo.AdjustImageLocation = New System.Drawing.Point(0, 0)
        Me.m_cmd_undo.BtnShape = SIS.Controls.Button.emunType.BtnShape.Rectangle
        Me.m_cmd_undo.BtnStyle = SIS.Controls.Button.emunType.XPStyle.[Default]
        Me.m_cmd_undo.Dock = System.Windows.Forms.DockStyle.Right
        Me.m_cmd_undo.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.m_cmd_undo.ImageIndex = 8
        Me.m_cmd_undo.ImageList = Me.imageList
        Me.m_cmd_undo.Location = New System.Drawing.Point(383, 0)
        Me.m_cmd_undo.Name = "m_cmd_undo"
        Me.m_cmd_undo.Size = New System.Drawing.Size(80, 34)
        Me.m_cmd_undo.TabIndex = 16
        Me.m_cmd_undo.Text = "&Undo"
        '
        'm_cmd_thoat
        '
        Me.m_cmd_thoat.AdjustImageLocation = New System.Drawing.Point(0, 0)
        Me.m_cmd_thoat.BtnShape = SIS.Controls.Button.emunType.BtnShape.Rectangle
        Me.m_cmd_thoat.BtnStyle = SIS.Controls.Button.emunType.XPStyle.[Default]
        Me.m_cmd_thoat.Dock = System.Windows.Forms.DockStyle.Right
        Me.m_cmd_thoat.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.m_cmd_thoat.ImageIndex = 6
        Me.m_cmd_thoat.ImageList = Me.imageList
        Me.m_cmd_thoat.Location = New System.Drawing.Point(463, 0)
        Me.m_cmd_thoat.Name = "m_cmd_thoat"
        Me.m_cmd_thoat.Size = New System.Drawing.Size(88, 34)
        Me.m_cmd_thoat.TabIndex = 15
        Me.m_cmd_thoat.Text = "&Thoát"
        Me.m_cmd_thoat.Visible = False
        '
        'm_stb_fms_style
        '
        Me.m_stb_fms_style.ImeMode = System.Windows.Forms.ImeMode.NoControl
        Me.m_stb_fms_style.Location = New System.Drawing.Point(0, 389)
        Me.m_stb_fms_style.Name = "m_stb_fms_style"
        Me.m_stb_fms_style.Panels.AddRange(New System.Windows.Forms.StatusBarPanel() {Me.m_stMess, Me.m_stUser})
        Me.m_stb_fms_style.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.m_stb_fms_style.ShowPanels = True
        Me.m_stb_fms_style.Size = New System.Drawing.Size(551, 22)
        Me.m_stb_fms_style.TabIndex = 19
        Me.m_stb_fms_style.Text = "statusBar"
        '
        'm_stMess
        '
        Me.m_stMess.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring
        Me.m_stMess.Icon = CType(resources.GetObject("m_stMess.Icon"), System.Drawing.Icon)
        Me.m_stMess.Name = "m_stMess"
        Me.m_stMess.Width = 503
        '
        'm_stUser
        '
        Me.m_stUser.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Contents
        Me.m_stUser.Icon = CType(resources.GetObject("m_stUser.Icon"), System.Drawing.Icon)
        Me.m_stUser.Name = "m_stUser"
        Me.m_stUser.Width = 31
        '
        'f108_phan_quyen_he_thong
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(551, 411)
        Me.Controls.Add(Me.m_fg)
        Me.Controls.Add(Me.m_panel)
        Me.Controls.Add(Me.m_stb_fms_style)
        Me.Name = "f108_phan_quyen_he_thong"
        Me.Text = "M108 - Phan quyen he thong School Management"
        CType(Me.m_fg, System.ComponentModel.ISupportInitialize).EndInit()
        Me.m_panel.ResumeLayout(False)
        CType(Me.m_stMess, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.m_stUser, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

#Region "Data Structures"
    Private Enum ColNumber
        Col_Ma_phan_quyen = 1
        Col_Loai_phan_quyen = 2
        Col_Ghi_Chu = 3
    End Enum

    Private Const C_PROCESS_LOCK As String = "PHAN_QUYEN_HE_THONG"

    Public Class HT_PHAN_QUYEN_HE_THONG
        Public Const MA_PHAN_QUYEN As String = "MA_PHAN_QUYEN"
        Public Const id As String = "ID"
        Public Const LOAI_PHAN_QUYEN As String = "LOAI_PHAN_QUYEN"
        Public Const GHI_CHU As String = "GHI_CHU"
    End Class

#End Region

#Region "Members"
    Private m_obj_transfer As ITransferDataRow
    Private m_ied_control_handler As C1InPlaceEditControl
    Private m_ds_ht_phan_quyen_he_thong As DS_HT_PHAN_QUYEN_HE_THONG
    Private m_bplock As CBusinessProcessLock
#End Region

#Region " PUBLIC INTERFACE "

    Public Sub display_phan_quyen_he_thong()
        Try
            m_bplock = New CBusinessProcessLock
            m_bplock.getLock(C_PROCESS_LOCK)
            Me.Show()
        Catch v_e As Exception
            CSystemLog_301.ExceptionHandle(v_e)
        End Try
    End Sub

    Public Sub display_in_panel() Implements I_DisplayableInPanel.display_in_panel
        Me.Show()
    End Sub


#End Region

#Region "Private methods"

    Private Sub formatControls()
        'CControlFormat.setFormStyle(Me, New CApplicationContext, IPFormStyle.DockableTopForm)
        CControlFormat.setFormStyle(Me, IPFormStyle.DockableTopForm)
        CControlFormat.setC1FlexFormat(m_fg, C1GridStyle.GridInplaceEditing)
        'add event handler
        AddHandler m_cmd_undo.Click, AddressOf Me.m_cmd_undo_Click
        AddHandler m_fg.ValidateEdit, AddressOf Me.m_fg_ValidateEdit
        AddHandler m_cmd_cap_nhat.Click, AddressOf Me.m_cmd_cap_nhat_Click
        AddHandler m_cmd_xoa.Click, AddressOf Me.m_cmd_xoa_Click
        AddHandler m_cmd_thoat.Click, AddressOf Me.m_cmd_thoat_Click
        'set dropdown

    End Sub

    Private Function get_trans_obj() As ITransferDataRow
        Dim v_hasttable As New Hashtable
        v_hasttable.Add(HT_PHAN_QUYEN_HE_THONG.MA_PHAN_QUYEN, ColNumber.Col_Ma_phan_quyen)
        v_hasttable.Add(HT_PHAN_QUYEN_HE_THONG.LOAI_PHAN_QUYEN, ColNumber.Col_Loai_phan_quyen)
        v_hasttable.Add(HT_PHAN_QUYEN_HE_THONG.GHI_CHU, ColNumber.Col_Ghi_Chu)
        Dim v_ds_ht_phan_quyen_he_thong As New DS_HT_PHAN_QUYEN_HE_THONG
        Dim v_obj_trans As New CC1TransferDataRow(m_fg, v_hasttable, v_ds_ht_phan_quyen_he_thong.HT_PHAN_QUYEN_HE_THONG.NewRow)
        Return v_obj_trans
    End Function

    Private Sub load_data()

        Dim v_us_ht_phan_quyen_he_thong As New US_HT_PHAN_QUYEN_HE_THONG
        Try
            m_ds_ht_phan_quyen_he_thong = New DS_HT_PHAN_QUYEN_HE_THONG
            v_us_ht_phan_quyen_he_thong.open_connection()
            v_us_ht_phan_quyen_he_thong.FillDataset(m_ds_ht_phan_quyen_he_thong)
            v_us_ht_phan_quyen_he_thong.commit_close_connection()
            m_obj_transfer = get_trans_obj()
            C1InPlaceEditControl.Dataset2C1Grid(m_ds_ht_phan_quyen_he_thong, m_fg, m_obj_transfer)
        Catch v_e As Exception
            v_us_ht_phan_quyen_he_thong.rollback_close_connection()
            Dim v_err_handler As New CDBExceptionHandler(v_e, New CDBClientDBExceptionInterpret)
            v_err_handler.showErrorMessage()
        End Try
    End Sub

    Private Sub undo_changes()
        m_ied_control_handler.undo_changes(Me.m_ds_ht_phan_quyen_he_thong, m_obj_transfer)
    End Sub

    Private Sub set_form_initial()
        m_ied_control_handler = New C1InPlaceEditControl(m_fg, Me)
        'm_fg.Cols(2).Editor = Me.m_c1dropdown_thuoc_Loai
        load_data()
    End Sub
#End Region


#Region "Inplace editing interface"

    Function is_empty_row(ByVal i_grid_row_index As Integer) As Boolean _
                                    Implements I_InplaceEditGridContainer.is_empty_row
        Return CGridUtils.are_there_no_visible_data_in_row(i_grid_row_index, m_fg)
    End Function

    Public Function get_updated_row_Object(ByVal i_grid_row_index As Integer) As InplaceEditedRowObject Implements I_InplaceEditGridContainer.get_updated_row_object
        'GET US OBJECT AND DATAROW
        Dim v_us As New US_HT_PHAN_QUYEN_HE_THONG
        Dim v_data_row As DataRow = CType(m_fg.Rows(i_grid_row_index).UserData, DataRow)
        v_us.DataRow2Me(v_data_row)
        'SET THE DATA HAVE BEEN EDITED IN THE ROW
        v_us.strGHI_CHU = Convert.ToString(m_fg(i_grid_row_index, ColNumber.Col_Ghi_Chu))
        v_us.strMA_PHAN_QUYEN = Convert.ToString(m_fg(i_grid_row_index, ColNumber.Col_Ma_phan_quyen))
        v_us.strLOAI_PHAN_QUYEN = Convert.ToString(m_fg(i_grid_row_index, ColNumber.Col_Loai_phan_quyen))
        'MAKE OBJECT EDITED
        Return New InplaceEditedRowObject(v_us, v_data_row, InPlaceEditRowState.Updated)
    End Function

    Public Function get_deleted_row_object(ByVal i_grid_row_index As Integer) As InplaceEditedRowObject Implements I_InplaceEditGridContainer.get_deleted_row_object
        Dim v_us As New US_HT_PHAN_QUYEN_HE_THONG
        Dim v_data_row As DataRow = CType(m_fg.Rows(i_grid_row_index).UserData, DataRow)
        v_us.DataRow2Me(v_data_row)
        Return New InplaceEditedRowObject(v_us, v_data_row, InPlaceEditRowState.Deleted)
    End Function

    Public Function get_new_row_object(ByVal i_grid_row_index As Integer) As InplaceEditedRowObject Implements I_InplaceEditGridContainer.get_new_row_object
        Dim v_us As New US_HT_PHAN_QUYEN_HE_THONG
        v_us.strGHI_CHU = Convert.ToString(m_fg(i_grid_row_index, ColNumber.Col_Ghi_Chu))
        v_us.strMA_PHAN_QUYEN = Convert.ToString(m_fg(i_grid_row_index, ColNumber.Col_Ma_phan_quyen))
        v_us.strLOAI_PHAN_QUYEN = Convert.ToString(m_fg(i_grid_row_index, ColNumber.Col_Loai_phan_quyen))
        Return New InplaceEditedRowObject(v_us, Nothing, InPlaceEditRowState.NewRow)
    End Function



#End Region

    '
    '
    'EVENT HANDLERS
    '
    '

    Private Sub m_cmd_thoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            Close()
        Catch v_e As Exception
            CSystemLog_301.ExceptionHandle(v_e)
        End Try
    End Sub


    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        Try
            set_form_initial()
        Catch v_e As Exception
            CSystemLog_301.ExceptionHandle(v_e)
        End Try
    End Sub




    Private Sub m_cmd_xoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            m_ied_control_handler.delete_current_row()
        Catch v_e As Exception
            CSystemLog_301.ExceptionHandle(v_e)
        End Try

    End Sub

    Private Sub m_cmd_cap_nhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            If Not m_ied_control_handler.finishEditingCurrentCell() Then Exit Sub
            m_ied_control_handler.apply_all_changes()
            m_ied_control_handler.reconcile_grid_data_with_dataset_and_database(m_ds_ht_phan_quyen_he_thong, m_obj_transfer)
            'Me.load_data()
            'm_ied_control_handler.reset_data()
        Catch v_e As Exception
            CSystemLog_301.ExceptionHandle(v_e)
        End Try
    End Sub


    Private Sub m_fg_ValidateEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.ValidateEditEventArgs)
        Try
            If Me.is_empty_row(e.Row) Then Exit Sub
            If (m_fg.Editor.Text.Trim().Length = 0) Then
                e.Cancel = True
            End If
        Catch ex As Exception
            CSystemLog_301.ExceptionHandle(ex)
        End Try
    End Sub


    Private Sub m_cmd_undo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        undo_changes()
    End Sub

    Protected Overrides Sub OnClosed(ByVal e As System.EventArgs)
        Try
            If Not m_bplock Is Nothing Then
                m_bplock.releaseLock()
            End If
        Catch ex As Exception
            CSystemLog_301.ExceptionHandle(ex)
        End Try
    End Sub

    Protected Overrides Sub OnClosing(ByVal e As System.ComponentModel.CancelEventArgs)
        If Not m_ied_control_handler.finishEditingCurrentCell Then Exit Sub
        Try
            If m_ied_control_handler.is_editing Then
                Select Case BaseMessages.MsgBox_YES_NO_CANCEL(101)
                    Case DialogResult.Yes
                        m_ied_control_handler.apply_all_changes()
                    Case DialogResult.Cancel
                        e.Cancel = True
                    Case DialogResult.No
                        'm_ied_control_handler.reset_data()
                        'e.Cancel = True
                End Select
            End If
        Catch v_e As Exception
            e.Cancel = True
        End Try
    End Sub



End Class

